﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
//
using System.Web.Services;
using DDSC.OracleADO;
using System.Collections;
using System.Text;
using System.Data;

public partial class krj_tracking : BasePermissionPage
{
    //維護動作
    public string Mode { get; set; }

    public Boolean History { get; set; }
    
    [WebMethod]
    public static Object GetDISPATCH(string CARGO_NO_S, string CARGO_NO_E, string DISPATCH_DATE, string SHIPPER, string SHIPPER_NAME, string RECEIVER, string PROJECT, string PAYER, string CUSTOMER, string DISPATCH_DEPT, string RECEIVED_DEPT, string s_TODAY, string sType, PageInfo p_PageInfo)
    {
        Object results = new Object();
        try
        {
            dbAccess m_dba = new dbAccess();
            StringBuilder m_sb = new StringBuilder();
            StringBuilder m_sbCount = new StringBuilder();
            StringBuilder m_sub_sb = new StringBuilder();
            ArrayList m_arrParam = new ArrayList();

            m_sb.Append(" select F63101 || F63102 SHIPPING_NO,C.DEPT_NAME DISPATCH_DEPT,D.DEPT_NAME RECEIVED_DEPT,F63103 DISPATCH_DATE,F63112 SHIPPER,F63114 RECEIVER,F63119 ITEM_COUNT ");
            m_sb.Append(" , H.F20602 STATUS "); //狀態
            m_sb.Append(" ,case when E.DCE701 is null then '' else '*' end  SING ");
            m_sb.Append(" ,substr(B.F61952,1) Remark  "); //是否轉超峰
            m_sb.Append(" , A.F63101 DETAIL_NO,A.F63102 ITEM_NO,B.F61904 CORRECT_NO ,F631A1 ORG_SHIPPING_NO");
            m_sb.Append(" from (select F63101,F63102,F63103,F63104,F63106,F63111,F63112,F63114,F63117,F63119,F631A1,F631B3,F631C5 ");
            m_sb.Append(" ,case when F63172 is not null  then F63172 else F63162 end STATUS,F631Z1 from AS400_ZDF631 ");
            m_sb.Append(" Where 1=1 ");
            //條件
            if (CARGO_NO_S != "")
            {
                m_sb.Append(" AND F63101 = :CARGO_NO_S ");
                m_sb.Append(" AND F63102 = :CARGO_NO_E ");
                m_arrParam.Add(m_dba.GetOracleParam(":CARGO_NO_S", CARGO_NO_S.Substring(0, 7)));
                m_arrParam.Add(m_dba.GetOracleParam(":CARGO_NO_E", CARGO_NO_S.Substring(7, 4)));

                m_sub_sb.Append(" AND F63101 = :CARGO_NO_S_1 ");
                m_sub_sb.Append(" AND F63102 = :CARGO_NO_E_1 ");
                

            }

            //發送日期
            if (DISPATCH_DATE != "")
            {
                m_sb.Append(" AND F63103 >= :DISPATCH_DATE ");
                m_arrParam.Add(m_dba.GetOracleParam(":DISPATCH_DATE", DISPATCH_DATE));

                m_sub_sb.Append(" AND F63103 >= :DISPATCH_DATE_1 ");
                
            }
            //託運人編號
            if (SHIPPER != "")
            {
                m_sb.Append(" AND F63111=:SHIPPER ");
                m_arrParam.Add(m_dba.GetOracleParam(":SHIPPER", SHIPPER));
            }
            //託運人名稱
            if (SHIPPER_NAME != "")
            {
                m_sb.Append(" AND F63112 like :SHIPPER_NAME ");
                m_arrParam.Add(m_dba.GetOracleParam(":SHIPPER_NAME", "%" + SHIPPER_NAME + '%'));
            }
            //收貨人名稱
            if (RECEIVER != "")
            {
                m_sb.Append(" AND F63114 like :RECEIVER ");
                m_arrParam.Add(m_dba.GetOracleParam(":RECEIVER", "%" + RECEIVER + '%'));

                m_sub_sb.Append("  AND F63114 like :RECEIVER_1 ");
            }
            //行銷專案
            if (PROJECT != "")
            {
                m_sb.Append(" AND F631B3 = :PROJECT ");
                m_arrParam.Add(m_dba.GetOracleParam(":PROJECT", PROJECT));
            }
            //付款人編號
            if (PAYER != "")
            {
                m_sb.Append(" AND F63117 = :PAYER ");
                m_arrParam.Add(m_dba.GetOracleParam(":PAYER", PAYER));
            }
            //客戶編號
            if (CUSTOMER != "")
            {
                m_sb.Append(" AND F63111 = :CUSTOMER ");
                m_arrParam.Add(m_dba.GetOracleParam(":CUSTOMER", CUSTOMER));

                m_sub_sb.Append(" AND F63111 = :CUSTOMER_1 ");
            }
            //發送所站
            if (DISPATCH_DEPT != "")
            {
                m_sb.Append(" AND F63104 = :DISPATCH_DEPT ");
                m_arrParam.Add(m_dba.GetOracleParam(":DISPATCH_DEPT", DISPATCH_DEPT));

                m_sub_sb.Append(" AND F63104 = :DISPATCH_DEPT_1 ");
            }
            //到著所站
            if (RECEIVED_DEPT != "")
            {
                m_sb.Append(" AND F63106 = :RECEIVED_DEPT ");
                m_arrParam.Add(m_dba.GetOracleParam(":RECEIVED_DEPT", RECEIVED_DEPT));

                m_sub_sb.Append(" AND F63106 = :RECEIVED_DEPT_1 ");
            }
            //當日/隔日配
            if (s_TODAY != "")
            {
                m_sb.Append(" AND F631C5 = :s_TODAY ");
                m_arrParam.Add(m_dba.GetOracleParam(":s_TODAY", s_TODAY));
            }

            m_sb.Append(" ) A  ");
            m_sb.Append(" left join  (select distinct F619Z1, F61901 ,F61902,first_value(F61904) over (partition by F619Z1 order by F61903 desc )  AS F61904, first_value(F61952)  over (partition by F619Z1 order by F61903 desc )  AS F61952 ");
            m_sb.Append(" from AS400_ZDF619 X ");
            //為了加快子查詢的速度
            if (m_sub_sb.Length > 0)
            {
                m_sb.Append(" where exists(select distinct F631A1 from AS400_ZDF631 Y  Where 1=1 " + m_sub_sb.ToString() + " and X.F619Z1 = Y.F631A1 )");
                //單號
                if (CARGO_NO_S != "")
                {
                    m_arrParam.Add(m_dba.GetOracleParam(":CARGO_NO_S_1", CARGO_NO_S.Substring(0, 7)));
                    m_arrParam.Add(m_dba.GetOracleParam(":CARGO_NO_E_1", CARGO_NO_S.Substring(7, 4)));
                }
                //發送日期
                if (DISPATCH_DATE != "")
                {
                    m_arrParam.Add(m_dba.GetOracleParam(":DISPATCH_DATE_1", DISPATCH_DATE));

                }
                //收貨人名稱
                if (RECEIVER != "")
                {
                    m_arrParam.Add(m_dba.GetOracleParam(":RECEIVER_1", "%" + RECEIVER + '%'));
                   
                }
                //客戶編號
                if (CUSTOMER != "")
                {
                    m_arrParam.Add(m_dba.GetOracleParam(":CUSTOMER_1", CUSTOMER));

                }
                //發送所站
                if (DISPATCH_DEPT != "")
                {
                    m_arrParam.Add(m_dba.GetOracleParam(":DISPATCH_DEPT_1", DISPATCH_DEPT));

                }
                //到著所站
                if (RECEIVED_DEPT != "")
                {
                    m_arrParam.Add(m_dba.GetOracleParam(":RECEIVED_DEPT_1", RECEIVED_DEPT));

                }
            }
            
            m_sb.Append("     ) B"); //取出最大更正序號
            m_sb.Append(" on A.F631A1 = B.F619Z1");
            m_sb.Append(" left join mis_dept_ap C on A.F63104 = C.DEPT_ID ");//發送所站
            m_sb.Append(" left join mis_dept_ap D on A.F63106 = D.DEPT_ID ");//到著所站
            m_sb.Append(" left join (select distinct DCE701 from as400_dcfe7 ) E on A.F631Z1 = E.DCE701 ");
            m_sb.Append(" left join as400_zdf206 H on A.STATUS = H.F20601 ");
            //m_sb.Append(" --History ");

                       
            results = ADOGridHandler(m_sb, m_arrParam, p_PageInfo);
            
            //20130628**********************************************
            //記錄最後查詢的條件
            krj_tracking sa = new krj_tracking();
            sa.savecondition( CARGO_NO_S,  CARGO_NO_E,  DISPATCH_DATE,  SHIPPER,  SHIPPER_NAME,  RECEIVER,  PROJECT,  PAYER,  CUSTOMER,  DISPATCH_DEPT,  RECEIVED_DEPT,  s_TODAY);
            
            //20130628**********************************************
            
        }
        catch (Exception ex)
        {
            results = ex.Message;
            //lbl_message.Text = BP_Message + ex.Message;
            BasePermissionPage BP = new BasePermissionPage();
            BP.ERROR_DBSAVE("krj_tracking.aspx.cs", "DBINT", ex.Message, "", "c#", "0", ex.StackTrace);
        }
        return (results);
    }
    

    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            Boolean a = false;
            a = his_flag;
            
            //Modify By Ben 20130621 修改傳遞方式=======
            //紀錄Grid裡面的Key用於傳遞到維護頁 Session 為程式名稱
            if (Session["krj_tracking"] != null)
            {
                SetKey("krj_tracking");
            }
            //String username = System.Security.Principal.WindowsIdentity.GetCurrent().Name;

            //發送日-Default 4天前
            txtDISPATCH_DATE.Text = string.Format("{0:yyyyMMdd}", DateTime.Now.AddDays(-4));
            //到著所站Default
            //Dept_ID = User.Identity.Dept_ID;

            DataSet ds = null;
            ds = DBINT();
            if (ds.Tables[0].Rows[0]["ReturnCode"].ToString() == "000")
            {
                //DropDownList資料bind入
                ddlDISPATCH_DEPT.DataTextField = "DEPT_NAME";
                ddlDISPATCH_DEPT.DataValueField = "DEPT_ID";
                ddlDISPATCH_DEPT.DataSource = ds.Tables["DEPT"];
                ddlDISPATCH_DEPT.DataBind();


                ddlRECEIVE_DEPT.DataTextField = "DEPT_NAME";
                ddlRECEIVE_DEPT.DataValueField = "DEPT_ID";
                ddlRECEIVE_DEPT.DataSource = ds.Tables["DEPT"];
                ddlRECEIVE_DEPT.DataBind();
            }
           
            //紀錄頁次
            var m_PageIndex = "1";
            var sType = "";
             //判斷如果是維護頁導回來就填回查詢條件
            if (Page.PreviousPage != null)
            {
                //處理查詢條件
                if (g_ConditionKey != null)
                {
                    sType = g_ConditionKey["sType"].ToString();

                    //txt_document_no.Value = g_ConditionKey["document_no"].ToString();
                    
                        //託運單號
                        txtCARGO_NO_S.Text = g_ConditionKey["CARGO_NO_S"].ToString();
                        //txtCARGO_NO_E.Value = g_ConditionKey["CARGO_NO_E"].ToString();
                        //發送日
                        txtDISPATCH_DATE.Text = g_ConditionKey["DISPATCH_DATE"].ToString();
                        //託運人編號
                        txtSHIPPER.Text = g_ConditionKey["SHIPPER"].ToString();
                        //託運人名稱
                        txtSHIPPER_NAME.Text = g_ConditionKey["SHIPPER_NAME"].ToString();
                        //收貨人名稱
                        txtRECEIVER.Text = g_ConditionKey["RECEIVER"].ToString();
                        //行銷專案
                        txtPROJECT.Text = g_ConditionKey["PROJECT"].ToString();
                        //付款人編號
                        txtPAYER.Text = g_ConditionKey["PAYER"].ToString();
                        //客戶編號
                        txtCUSTOMER.Text = g_ConditionKey["CUSTOMER"].ToString();
                        //發送所站
                        //txtDISPATCH_DEPT.Value = g_ConditionKey["DISPATCH_DEPT"].ToString();
                        ddlDISPATCH_DEPT.SelectedValue = g_ConditionKey["DISPATCH_DEPT"].ToString(); ;
                        //到著所站
                        //txtRECEIVE_DEPT.Value = g_ConditionKey["RECEIVED_DEPT"].ToString();
                        ddlRECEIVE_DEPT.SelectedValue = g_ConditionKey["RECEIVED_DEPT"].ToString();

                }
                //執行搜尋功能
                ClientScript.RegisterClientScriptBlock(this.GetType(), "btnSearch_Click", "<script language='javascript'>$(document).ready(function () {btnSearch_Click('" + m_PageIndex + "'); }) </script>");
            }
        }
        catch (Exception ex)
        {
            //Response.Write(e.Message);
            lbl_message.Text = BP_Message  + ex.Message;
            ERROR_DBSAVE(Page.ToString(), e.ToString(), ex.Message, "", "c#", "0", ex.StackTrace);
            
        }

        
    }
    protected void toPage()
    {
        //記錄是否為歷史資料
        History = his_flag;
        Server.Transfer("~/Management/Tracking/krj_tracking_detail.aspx", false);
        
    }
    protected void btnADD_Click(object sender, EventArgs e)
    {
        Mode = "Insert";
        toPage();
    }

    protected void btnFIND_Click(object sender, EventArgs e)
    {
        Mode = "View";
        toPage();
    }

    protected void btnEDIT_Click(object sender, EventArgs e)
    {
        Mode = "Update";
        toPage();

    }

    protected void btnDEL_Click(object sender, EventArgs e)
    {
        Mode = "Delete";
        toPage();

    }

    protected void btn_VIEW_Click(object sender, EventArgs e)
    {
        Mode = "View";
        toPage();
    }

    protected DataSet DBINT()
    {
        DataSet ds = null;
        try
        {
            dbAccess m_dba = new dbAccess();
            StringBuilder m_sb = new StringBuilder();
            ArrayList m_arrParam = new ArrayList();

            m_sb.Append(" select A.bi4101 DEPT_ID ,A.bi4101 || '-' || B.DEPT_NAME DEPT_NAME ");
            m_sb.Append(" from as400_bif41 A left join MIS_DEPT_AP  B on A.BI4101 = B.DEPT_ID ");
            m_sb.Append(" where BI4121 = '1' and BI4122 = '2' ");
            m_sb.Append(" order by A.bi4101 ");
            
            ds = m_dba.runExecuteQuery("DEPT", m_sb.ToString(), m_arrParam);
            //if (ds.Tables[0].Rows[0]["ReturnCode"].ToString() != "000")
            //{
            //    lbl_message.Text = "as400_bif41: 資料讀取錯誤！" + ds.Tables[0].Rows[0]["LogMessage"];
            //}
           
        }
        catch (Exception ex)
        {
            lbl_message.Text = BP_Message + ex.Message;
            ERROR_DBSAVE(Page.ToString(), "DBINT", ex.Message, "", "c#", "0", ex.StackTrace);
        }
        return ds;
    }
    protected void btn_exist_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/Default.aspx");
    }
    protected void savecondition(string CARGO_NO_S, string CARGO_NO_E, string DISPATCH_DATE, string SHIPPER, string SHIPPER_NAME, string RECEIVER, string PROJECT, string PAYER, string CUSTOMER, string DISPATCH_DEPT, string RECEIVED_DEPT, string s_TODAY)
    {
        try
        {
            DropDownList ddl = new DropDownList();
            TextBox tx = new TextBox();
            CheckBox ck = new CheckBox();
            


            var controlItem = new List<ControlItem>() 
            {
              new ControlItem("txtCARGO_NO_S", CARGO_NO_S, tx.GetType()),
              new ControlItem("txtDISPATCH_DATE", DISPATCH_DATE, tx.GetType()),
              new ControlItem("txtSHIPPER", SHIPPER, tx.GetType()),
              new ControlItem("txtSHIPPER_NAME", SHIPPER_NAME, tx.GetType()),
              new ControlItem("txtRECEIVER", RECEIVER, tx.GetType()),
              new ControlItem("txtPROJECT", PROJECT, tx.GetType()),
              new ControlItem("chkToday", s_TODAY, ck.GetType()),
              new ControlItem("txtPAYER", PAYER, tx.GetType()),
              new ControlItem("txtCUSTOMER", CUSTOMER, tx.GetType()),
              new ControlItem("ddlDISPATCH_DEPT", DISPATCH_DEPT, ddl.GetType()),
              new ControlItem("ddlRECEIVE_DEPT", RECEIVED_DEPT, ddl.GetType())   
            };
            ConditionRecordUtil.RecordSearchConditionData(controlItem);
        }
        catch (Exception ex)
        {
            ERROR_DBSAVE("krj_tracking.aspx.cs", "savecondition", ex.Message, "", "c#", "0", ex.StackTrace);
        }
    }
    
}
